﻿using System;

using DreamCube.Foundation.Basic.Utility;
using DreamCube.Framework.Utilities.BasicObject;

namespace DreamCube.WebServer.WinServiceModel
{
    public static class Common
    {
        /// <summary>
        /// IIS的进程名
        /// </summary>
        public static String ProcessName = "SouthIIS";
        /// <summary>
        /// 配置数据保存在注册表中的项名
        /// </summary>
        public static String ConfigDataRegistryKey = @"SOFTWARE\South\SINFO";
        /// <summary>
        /// 配置数据保存所在的属性名
        /// </summary>
        public static String ConfigDataPropertyName = "SouthIIS";
        public static String ConfigDataPropertyName_KeepLive = "SouthIISKeepLiveData";
        /// <summary>
        /// windows服务名称
        /// </summary>
        public static String ServiceName = "SouthIIS";

        /// <summary>
        /// 守护进程的服务名
        /// </summary>
        public static String KeepLiveServiceName = "SouthIISKeepLiveServer";

        /// <summary>
        /// 服务程序的Exe名称
        /// </summary>
        public static String ServiceEXEFileName = "DreamCube.WebServer.WinService.exe";

        /// <summary>
        /// 这个是配置类
        /// </summary>
        private static KeepLiveServerConfig keepLiveServerConfig = null;
        private static ServerConfig serverConfig = null;

        /// <summary>
        /// IIS进程名（从apppath中分析出来的）
        /// </summary>
        public static String IISProcessName
        {
            get
            {
                if (keepLiveServerConfig == null)
                    keepLiveServerConfig = ObjectXHelper.CreateObjFromJSONRegistry_LocalMachine<KeepLiveServerConfig>(ConfigDataRegistryKey, ConfigDataPropertyName_KeepLive);
                if (keepLiveServerConfig != null)
                    return MyString.LeftOfLast(MyString.RightOfLast(keepLiveServerConfig.IISAppPath, "\\"), ".");
                return String.Empty;
            }
        }

        /// <summary>
        /// 网站中，通信确保网站存货的页面路径
        /// </summary>
        public static String WebLivePagePath
        {
            get
            {
                if (serverConfig == null)
                    serverConfig = ObjectXHelper.CreateObjFromJSONRegistry_LocalMachine<ServerConfig>(ConfigDataRegistryKey, ConfigDataPropertyName);
                if (serverConfig != null)
                    return String.Format("http://localhost:{0}/KeepWebLive.aspx", serverConfig.Port.ToString());
                return String.Empty;
            }
        }

        /// <summary>
        /// 网站中，通信确保网站存货的页面路径
        /// </summary>
        public static String StartIISPagePath
        {
            get
            {
                if (serverConfig == null)
                    serverConfig = ObjectXHelper.CreateObjFromJSONRegistry_LocalMachine<ServerConfig>(ConfigDataRegistryKey, ConfigDataPropertyName);
                if (serverConfig != null)
                {
                    String url = serverConfig.MainServerUrl;
                    if (!url.EndsWith("/"))
                        url += "/";
                    return String.Format("{0}StartIISApp.aspx", url);
                }
                return String.Empty;
            }
        }

        /// <summary>
        /// iis的程序路径
        /// </summary>
        public static String IISAppPath
        {
            get
            {
                if (keepLiveServerConfig == null)
                    keepLiveServerConfig = ObjectXHelper.CreateObjFromJSONRegistry_LocalMachine<KeepLiveServerConfig>(ConfigDataRegistryKey, ConfigDataPropertyName_KeepLive);
                if (keepLiveServerConfig != null)
                    return keepLiveServerConfig.IISAppPath;
                return String.Empty;
            }
        }
    }
}
    

